package sa4

import javax.servlet.http._
import scala.xml._
import scala.collection.JavaConversions._
import scala.util.matching.Regex
import javax.servlet.ServletConfig
import java.net._
import scala.xml._
import scala.io._
import java.io.FileInputStream
import java.io.File


// This is a very simple Scala servlet.
// If you rename this class, you must also change the file
// src/main/webapp/WEB-INF/web.xml to use the new name.
class mainFramework extends HttpServlet {


  override def service(req: HttpServletRequest, resp: HttpServletResponse) {
    
    
    val feed = "^/feed/.+$".r
    val search = "^/search/$".r
    
  
  val pp = new PrettyPrinter(72,2)
    
	req.getRequestURI match {
	case feed() =>
	  	val input = req.getRequestURI.split("/")
	  	val url = new URL("https://api.twitter.com/1/statuses/user_timeline.xml?include_entities=true&include_rts=true&screen_name="+ input(2) + "&count=100")
	  	val ns = XML.load(url)
	  	val result = ns \\ "status"
	    //resp.getWriter.write("<html><body>"+pp.formatNodes(ns)+"</body></html>")
	  	resp.getWriter.write(renderPageFeed(result).toString)
	case search() =>
	  	val searched = req.getParameter("q")
	  	val url = new URL("http://search.twitter.com/search.atom?q=" + searched)
	  	val ns = XML.load(url)
	  	val entries = ns \\ "entry"
	  	
	  	//tuple(autore, content, published, title, name)
	  	// for(element <- result) yield element._1 + element._2, element._3 + ....
	  	
	  	//val result = {entries map { x => {x \\ "title"} } }.text
	  	//val entries = (ns \\ "entry").toList.text
	  	//val result = "<html><body>"+pp.formatNodes(ns)+"</body></html>"
	  	resp.getWriter.write(renderPageSearch(entries).toString)
	    //resp.getWriter.write(pp.formatNodes(ns ))
	  	//resp.getWriter.write(pp.formatNodes(renderPage2(ns)))
	case _ =>
      	val s = ((scala.io).Source).fromFile("/Users/Nicolas/Desktop/simple-scala-servlet/src/main/scala/sa4/some_text.txt")
      	s.getLines.foreach( (line) => {
      		resp.getWriter.write(line)
      	})
      
        //resp.getWriter.write(new PrettyPrinter(72, 2).formatNodes(renderPage(req)))
    }
  }

  def renderPageFeed(entries: NodeSeq) : NodeSeq ={ 
    val results = for(element <- entries) yield(element \ "text")
    println(results.length)
    <html>
      <head>
    	<link rel="stylesheet" href="/main.css"/>
        <title>Feed</title>
      </head>
      <body>
		   { for(element <- results) yield (<span> Text: {element}</span><hr/>) }
      </body>
    </html>
  
  }

  		

   def renderPageSearch(entries: NodeSeq) : NodeSeq = {
     var counter=0
   	val results = for(element <- entries) yield(element \ "author", element \ "content", element \ "published", element \ "title", element \ "name")
   	
	  for(element <- entries){
	    counter += 1
	  }	
     print(counter)
     var listOfAuthor =new Array [NodeSeq](counter: Int)
     var aCounter = 0
     
     for(element <- results){
       
       if(aCounter == 0){
         
         
    	   listOfAuthor.update(0, element._1)
    	   aCounter += 1
       }
       else{ 
    	   var currentAuthor = element._1
    	   var found = false
		   for(author<-listOfAuthor){
			   if((element._1)==author && !found){
				   found = true
			   }
		   }
    	   if(!found){
    		   listOfAuthor.update(aCounter, currentAuthor)
			   aCounter += 1
    	   }
       }
     }
    <html>
      <head>
     	<link rel="stylesheet" href="main.css"/>
        <title>Search</title>
      </head>
      <body>
     		<p>matched {counter} tweets and  {aCounter} users</p>
		   { for(element <- results) yield (<span> author: {element._1} - {element._2} - {element._3} - {element._4} - {element._5}</span><hr/>) }
      </body>
    </html>
   }
}
